<template>
  <div class="bruce">
    <ul class="bubble-bgwall">
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
      <li>Grneral</li>
    </ul>
  </div>
  <div class="forgot-password-container">
    <div class="forgot-password-box">
      <div class="forgot-password-header">
        <h1>忘记密码</h1>
      </div>
      <form @submit.prevent="handleForgotPassword" class="forgot-password-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input id="username" v-model="username" type="text" placeholder="请输入用户名" required />
        </div>
        <div class="form-group">
          <label for="newPassword">新密码</label>
          <input id="newPassword" v-model="newPassword" type="password" placeholder="请输入新密码" required />
        </div>
        <div class="form-footer">
          <button type="submit" class="submit-button">重置密码</button>
          <button type="button" @click="handleBackToLogin" class="back-button">返回登录</button>
        </div>
        <div v-if="error" class="error-message">
          {{ error }}
        </div>
        <div v-if="success" class="success-message">
          {{ success }}
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

axios.defaults.baseURL = 'http://localhost:3000';

export default {
  setup() {
    const router = useRouter();
    const username = ref('');
    const newPassword = ref('');
    const error = ref('');
    const success = ref('');

    const handleForgotPassword = async () => {
      if (!username.value || !newPassword.value) {
        error.value = '请输入用户名和新密码';
        success.value = '';
        return;
      }
      try {
        const response = await axios.post('/forgotPassword', {
          username: username.value,
          newPassword: newPassword.value
        });
        if (response.data.success) {
          success.value = '密码重置成功，请返回登录';
          error.value = '';
        } else {
          error.value = response.data.message || '重置失败';
          success.value = '';
        }
      } catch (err) {
        if (err.response && err.response.data && err.response.data.message) {
          error.value = err.response.data.message;
        } else {
          error.value = '发生错误，请稍后再试。';
        }
        success.value = '';
      }
    };

    const handleBackToLogin = () => {
      router.push({ name: 'Login' });
    };

    return {
      username,
      newPassword,
      error,
      success,
      handleForgotPassword,
      handleBackToLogin
    };
  }
};
</script>

<style scoped>
html, body, #app {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
}

.bruce {
  height: 100vh;
  width: 100vw;
  background-image: linear-gradient(270deg, #8146b4, #6990f6);
  position: fixed;
  top: 0;
  left: 0;
  z-index: 0;
  overflow: hidden;
}

.bubble-bgwall {
  overflow: hidden;
  position: absolute;
  width: 100vw;
  height: 100vh;
  padding: 0;
  margin: 0;
  top: 0;
  left: 0;
  z-index: 1;
}

.bubble-bgwall li {
  display: flex;
  position: absolute;
  bottom: -200px;
  justify-content: center;
  align-items: center;
  border-radius: 10px;
  width: 50px;
  height: 50px;
  background-color: rgba(255, 255, 255, 0.15);
  color: #ccc;
  animation: bubble 15s infinite;
}

.bubble-bgwall li:nth-child(1) { left: 10%; }
.bubble-bgwall li:nth-child(2) { left: 20%; width: 90px; height: 90px; animation-duration: 7s; animation-delay: 2s; }
.bubble-bgwall li:nth-child(3) { left: 25%; animation-delay: 4s; }
.bubble-bgwall li:nth-child(4) { left: 40%; width: 60px; height: 60px; background-color: rgba(255,255,255,0.3); animation-duration: 8s; }
.bubble-bgwall li:nth-child(5) { left: 70%; }
.bubble-bgwall li:nth-child(6) { left: 80%; width: 120px; height: 120px; background-color: rgba(255,255,255,0.2); animation-delay: 3s; }
.bubble-bgwall li:nth-child(7) { left: 32%; width: 160px; height: 160px; animation-delay: 2s; }
.bubble-bgwall li:nth-child(8) { left: 55%; width: 40px; height: 40px; font-size: 12px; animation-duration: 15s; animation-delay: 4s; }
.bubble-bgwall li:nth-child(9) { left: 25%; width: 40px; height: 40px; background-color: rgba(255,255,255,0.3); font-size: 12px; animation-duration: 12s; animation-delay: 2s; }
.bubble-bgwall li:nth-child(10) { left: 85%; width: 160px; height: 160px; animation-delay: 5s; }

@keyframes bubble {
  0% { opacity: 0.5; transform: translateY(0) rotate(45deg); }
  25% { opacity: 0.75; transform: translateY(-400px) rotate(90deg); }
  50% { opacity: 1; transform: translateY(-600px) rotate(135deg); }
  100% { opacity: 0; transform: translateY(-1000px) rotate(180deg); }
}

.forgot-password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  position: relative;
  z-index: 2;
  background: none;
}

.forgot-password-box {
  background: rgba(255,255,255,0.98);
  padding: 38px 38px 28px 38px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(25, 118, 210, 0.13);
  width: 360px;
  max-width: 92vw;
  display: flex;
  flex-direction: column;
  align-items: center;
  animation: fadeIn 0.6s;
}

.forgot-password-header {
  text-align: center;
  margin-bottom: 24px;
}

.forgot-password-header h1 {
  font-size: 26px;
  color: #1976d2;
  font-weight: 700;
  letter-spacing: 1px;
}

.forgot-password-form {
  display: flex;
  flex-direction: column;
  width: 100%;
}

.form-group {
  margin-bottom: 18px;
  display: flex;
  flex-direction: column;
}

.form-group label {
  margin-bottom: 6px;
  font-weight: 500;
  color: #1976d2;
  font-size: 15px;
}

.form-group input {
  padding: 10px 12px;
  border: 1.5px solid #e3e8ee;
  border-radius: 6px;
  font-size: 15px;
  background: #f8fafc;
  transition: border 0.2s;
}

.form-group input:focus {
  border: 1.5px solid #1976d2;
  outline: none;
}

.form-footer {
  display: flex;
  justify-content: space-between;
  margin-top: 18px;
}

.submit-button, .back-button {
  padding: 10px 22px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 15px;
  font-weight: 500;
  transition: background 0.2s, box-shadow 0.2s;
  box-shadow: 0 2px 8px rgba(25,118,210,0.08);
}

.submit-button {
  background: linear-gradient(90deg, #1976d2 60%, #90caf9 100%);
  color: white;
}

.back-button {
  background: linear-gradient(90deg, #f44336 60%, #ff7961 100%);
  color: white;
}

.submit-button:hover {
  background: linear-gradient(90deg, #1256a3 60%, #90caf9 100%);
}

.back-button:hover {
  background: linear-gradient(90deg, #d32f2f 60%, #ff7961 100%);
}

.error-message {
  color: #f44336;
  margin-top: 12px;
  text-align: center;
  font-size: 15px;
}

.success-message {
  color: #43a047;
  margin-top: 12px;
  text-align: center;
  font-size: 15px;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: none; }
}
</style>
